package com.twu.biblioteca;

import sun.util.logging.resources.logging_it;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;
import java.util.Scanner;

public class BibliotecaApp {

    private static Library library = new Library();

    public static String WelcomeMsg(){
       return("Welcome to Biblioteca");
    }
    public static String DisplayMenu(){
        return(" 1. List Books\n 2. Check Out Book\n 3. Return Book\n 4. List Movies\n 5. Check Out Movie\n 6. View User Information");
    }
    public static Operation MenuSelect(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int choice;
        while(true){
            try{
                choice = Integer.parseInt(br.readLine());
            }
            catch (Exception e){
                System.out.println("Select a valid option!");
                continue;
            }
            if(choice==1)
                return Operation.listBook;
            else if(choice==2)
                return Operation.checkOutBook;
            else if (choice==3)
                return Operation.returnBook;
            else if (choice==4)
                return Operation.listMovie;
            else if (choice==5)
                return Operation.checkOutMovie;
            else if(choice==6)
                return Operation.viewUserInfo;
            else
                System.out.println("Select a valid option!");
        }

    }

    public static Customer AuthenticateUser(){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        Customer LoggedInCustomer = null;
        while(true){
            try {
                System.out.println("Enter Login Credentials");
                System.out.print("Library Number : ");
                String id = br.readLine();
                System.out.print("Password : ");
                String password = br.readLine();
                LoggedInCustomer=library.AuthenticateUser(id,password);
                if(LoggedInCustomer!=null)
                    return LoggedInCustomer;
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("Invalid Login!!!!");
        }
    }

    public static void MenuAction(Operation operation){
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        switch (operation){
            case listBook:
                List<Book> bookList = library.listBooks();
                System.out.println("Books in the Library are :");
                for(Book book: bookList){
                    System.out.println(book.Name);
                }
                break;

            case checkOutBook:
                try {
                    Customer LoggedInCustomer=AuthenticateUser();
                    System.out.println("Enter name of Book you wish to borrow");
                    String bookToBorrow = br.readLine();
                    System.out.println(library.checkOutBook(LoggedInCustomer.getName(), bookToBorrow));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

            case returnBook:
                try {
                    Customer LoggedInCustomer=AuthenticateUser();
                    System.out.println("Enter name of Book you wish to Return");
                    String bookToReturn = br.readLine();
                    System.out.println(library.returnBook(LoggedInCustomer.getName(), bookToReturn));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

            case listMovie:
                List<Movie> movieList = library.listMovies();
                System.out.println("Movies in the Library are :");
                for(Movie movie: movieList){
                    System.out.println(movie.getName());
                }
                break;

            case checkOutMovie:
                try {
                    Customer LoggedInCustomer=AuthenticateUser();
                    System.out.println("Enter name of Movie you wish to borrow");
                    String movieToBorrow = br.readLine();
                    System.out.println(library.checkOutMovie(LoggedInCustomer.getName(), movieToBorrow));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                break;

            case viewUserInfo:
                Customer LoggedInCustomer=AuthenticateUser();
                System.out.println("Name : "+LoggedInCustomer.getName());
                System.out.println("Email : "+LoggedInCustomer.getEmail());
                System.out.println("Phone No : "+LoggedInCustomer.getPhoneNo());
                break;

        }
    }
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println(WelcomeMsg());
        String ch = "Y";
        while (ch.equals("y")|| ch.equals("Y")){
            System.out.println(DisplayMenu());
            MenuAction(MenuSelect());
            System.out.println("Do you want to continue? (Y/N)");
            ch = sc.next();
        }
    }
}
